package com.futao.springbootlearn20220224.aware;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.springframework.context.ResourceLoaderAware;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.List;

/**
 * 可以通过这种方式来读取classpath，fileSystem的文件
 *
 * @author futao@gmail.com
 * @date 2022/3/9
 */
@Slf4j
@Component
public class ResourceLoaderAwareDemo implements ResourceLoaderAware {
    @Override
    public void setResourceLoader(ResourceLoader resourceLoader) {
        Resource resource = resourceLoader.getResource("classpath:i18n/message.properties");
        if (resource.exists()) {
            try {
                List<String> contentLines = Files.readAllLines(resource.getFile().toPath(), StandardCharsets.UTF_8);
                log.info("读取文件:{}", contentLines);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
